// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MyString.java

package org.quickserver.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.text.DecimalFormat;

public class MyString
{

    public MyString()
    {
    }

    public static String replace(String source, String key, String with)
    {
        if(source == null)
            throw new NullPointerException("Parameter -> source was null");
        if(key == null)
            throw new NullPointerException("Parameter -> key was null");
        if(with == null)
            throw new NullPointerException("Parameter -> with was null");
        int start = 0;
        int end = 0;
        String result = "";
        start = source.indexOf(key);
        end = start + key.length();
        if(start == -1)
        {
            return null;
        } else
        {
            result = source.substring(0, start);
            result = (new StringBuilder(String.valueOf(result))).append(with).toString();
            result = (new StringBuilder(String.valueOf(result))).append(source.substring(end, source.length())).toString();
            return result;
        }
    }

    public static String replaceAll(String source, String key, String with)
    {
        if(source == null)
            throw new NullPointerException("Parameter -> source was null");
        if(key == null)
            throw new NullPointerException("Parameter -> key was null");
        if(with == null)
            throw new NullPointerException("Parameter -> with was null");
        String temp = "";
        do
        {
            temp = "";
            temp = replace(source, key, with);
            if(temp != null)
                source = temp;
            else
                return source;
        } while(true);
    }

    public static int replaceCount(String source, String key)
    {
        if(source == null)
            throw new NullPointerException("Parameter -> source was null");
        if(key == null)
            throw new NullPointerException("Parameter -> key was null");
        int count = 0;
        String result = "";
        String temp = "";
        result = source;
        do
        {
            temp = "";
            temp = replace(result, key, "");
            if(temp != null)
            {
                result = temp;
                count++;
            } else
            {
                return count;
            }
        } while(true);
    }

    public static String replaceAllNo(String source, String with)
    {
        if(source == null)
            throw new NullPointerException("One of parameter -> source was null");
        if(with == null)
            throw new NullPointerException("One of parameter -> with was null");
        for(int i = 0; i < 10; i++)
            source = replaceAll(source, (new StringBuilder()).append(i).toString(), with);

        return source;
    }

    public static String removeAllHtmlSpChar(String source)
    {
        String temp = source;
        temp = replaceAll(temp, "&nbsp;", " ");
        temp = replaceAll(temp, "&lt;", "<");
        temp = replaceAll(temp, "&gt;", ">");
        temp = replaceAll(temp, "&amp;", "&");
        temp = replaceAll(temp, "&quot;", "\"");
        return temp;
    }

    public static String replaceTags(String source, String with)
    {
        if(source == null)
            throw new NullPointerException("One of parameter -> source was null");
        if(with == null)
            throw new NullPointerException("One of parameter -> with was null");
        int start = 0;
        int end = 0;
        int error = 0;
        String result = "";
        start = source.indexOf("<");
        end = source.indexOf(">", start + 1);
        error = source.indexOf("<", start + 1);
        if(error != -1 && error < end)
            throw new IllegalArgumentException("&lt; found before &gt;");
        if(start == -1 || end == -1)
        {
            return null;
        } else
        {
            result = source.substring(0, start);
            result = (new StringBuilder(String.valueOf(result))).append(with).toString();
            result = (new StringBuilder(String.valueOf(result))).append(source.substring(end + 1, source.length())).toString();
            return result;
        }
    }

    public static String replaceAllTags(String source, String with)
    {
        if(source == null)
            throw new NullPointerException("One of parameter -> source was null");
        if(with == null)
            throw new NullPointerException("One of parameter -> with was null");
        String temp = "";
        do
        {
            temp = "";
            temp = replaceTags(source, with);
            if(temp != null)
                source = temp;
            else
                return source;
        } while(true);
    }

    public static String getStackTrace(Throwable e)
    {
        StringWriter writer = new StringWriter(1024);
        e.printStackTrace(new PrintWriter(writer));
        return writer.toString();
    }

    public static String getMemInfo(float bytes)
    {
        if(bytes < 1024F)
            return (new StringBuilder(String.valueOf(doublePrcNum.format(bytes)))).append(" B").toString();
        bytes /= 1024F;
        if(bytes < 1024F)
            return (new StringBuilder(String.valueOf(doublePrcNum.format(bytes)))).append(" KB").toString();
        bytes /= 1024F;
        if(bytes < 1024F)
        {
            return (new StringBuilder(String.valueOf(doublePrcNum.format(bytes)))).append(" MB").toString();
        } else
        {
            bytes /= 1024F;
            return (new StringBuilder(String.valueOf(doublePrcNum.format(bytes)))).append(" GB").toString();
        }
    }

    public static String getSystemInfo(String version)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("---- System Info Start ---");
        sb.append("\r\n");
        sb.append("QuickServer v");
        sb.append(version);
        sb.append(" is being used.");
        sb.append("\r\n");
        sb.append("Java VM v");
        sb.append(System.getProperty("java.version"));
        sb.append(" is being used.");
        sb.append("\r\n");
        sb.append("Operating System: ");
        sb.append(System.getProperty("os.name"));
        sb.append(" ");
        sb.append(System.getProperty("os.version"));
        sb.append("\r\n");
        sb.append("Current working directory: ");
        sb.append(System.getProperty("user.dir"));
        sb.append("\r\n");
        sb.append("Class/s loaded from: ");
        sb.append((new MyString()).getClass().getProtectionDomain().getCodeSource().getLocation());
        sb.append("\r\n");
        sb.append("Total memory currently available: ");
        sb.append(getMemInfo(runtime.totalMemory()));
        sb.append("\r\n");
        sb.append("Memory currently in use: ");
        sb.append(getMemInfo(runtime.totalMemory() - runtime.freeMemory()));
        sb.append("\r\n");
        sb.append("Maximum memory available: ");
        sb.append(getMemInfo(runtime.maxMemory()));
        sb.append("\r\n");
        sb.append("---- System Info End ---");
        return sb.toString();
    }

    public static String alignRight(String data, int len)
    {
        StringBuilder sb;
        for(sb = new StringBuilder(data); sb.length() < len; sb.insert(0, ' '));
        return sb.toString();
    }

    public static String alignLeft(String data, int len)
    {
        StringBuilder sb;
        for(sb = new StringBuilder(data); sb.length() < len; sb.append(' '));
        return sb.toString();
    }

    private static Runtime runtime = Runtime.getRuntime();
    private static DecimalFormat doublePrcNum = new DecimalFormat("#,##0.00");

}
